package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.dubbo.mappers.UmsMenuMapper;
import com.tanhua.model.domain.UmsMenu;

import com.mall.commons.exceotion.ErrorResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class UmsMenuApiImpl implements UmsMenuApi {


    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Override
    public List<UmsMenu> findByIds(List<Long> menuIds) {
        if (menuIds.size() == 0) {
            return null;
        }
        return umsMenuMapper.selectBatchIds(menuIds);
    }

    /**
     * 分页查询后台菜单
     *
     * @param pageNum
     * @param pageSize
     * @param parentId
     * @return
     */
    @Override
    public Page listGetById(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<UmsMenu> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(null != parentId, UmsMenu::getParentId, parentId);
        return umsMenuMapper.selectPage(page, queryWrapper);
    }

    /**
     * 根据ID获取菜单详情
     *
     * @param id
     * @return
     */
    @Override
    public UmsMenu getById(Integer id) {
        if (id != null) {
            return umsMenuMapper.selectById(id);
        }
        return null;
    }

    /**
     * 根据ID删除后台菜单
     *
     * @param id
     * @return
     */
    @Override
    public boolean deletes(Integer id) {
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != id, UmsMenu::getId, id);
        UmsMenu umsMenu = umsMenuMapper.selectOne(queryWrapper);
        if (umsMenu.getParentId() == 0) {
            LambdaQueryWrapper<UmsMenu> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(UmsMenu::getParentId, umsMenu.getId());
            Integer integer = umsMenuMapper.selectCount(queryWrapper2);
            if (integer > 0) {
                throw new BusinessException(ErrorResult.serveError());
            }

        }
        return umsMenuMapper.deleteById(id) > 0 ? true : false;
    }


    /**
     * 修改菜单显示状态
     *
     * @param id
     * @param hidden
     * @return
     */
    @Override
    public boolean updateHiddenById(Integer id, Integer hidden) {
        if (id != null) {
            UmsMenu umsMenu = umsMenuMapper.selectById(id);
            if (umsMenu != null) {
                umsMenu.setHidden(hidden);
                umsMenuMapper.updateById(umsMenu);
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 添加后台菜单
     *
     * @param umsMenu
     * @return
     */
    @Override
    public boolean creates(UmsMenu umsMenu) {
        if (umsMenu == null) {
            return false;
        }
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getTitle, umsMenu.getTitle());
        if (umsMenuMapper.selectCount(wrapper) > 0) {
            throw new BusinessException("id重复请重新选择!");
        }
        if (umsMenu.getParentId() == 0) {
            umsMenu.setCreateTime(new Date(System.currentTimeMillis()));
            umsMenu.setLevel(0);
        } else {
            umsMenu.setCreateTime(new Date(System.currentTimeMillis()));
            umsMenu.setLevel(1);
        }

        return umsMenuMapper.insert(umsMenu) > 0 ? true : false;
    }

    /**
     * 树形结构返回所有菜单列表,返回一级和子级
     *
     * @return
     */
    @Override
    public List<UmsMenu> treeListAll() {
        return umsMenuMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 修改后台菜单
     *
     * @param umsMenu
     * @return
     */
    @Override
    public boolean updatess(UmsMenu umsMenu) {
        if (umsMenu != null) {
            umsMenuMapper.updateById(umsMenu);
            return true;
        }
        return false;
    }
}
